/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.IndexColumn;

import ar.com.greensoft.neo.web.jsf.util.ResourceBundleUtil;

/**
 *
 * @author Jorge E. Villaverde
 * @since 1.0
 */
@Entity
@Table(name="groups")
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQueries({
	@NamedQuery(name=Group.FIND_ALL_QUERY_NAME, query="SELECT a FROM Group a ORDER BY a.name"),
	@NamedQuery(name=Group.COUNT_QUERY_NAME,    query="SELECT COUNT(a) FROM Group a"),
})
@AttributeOverrides({
	@AttributeOverride(name="name", column=@Column(name="group_name")),	
})
public class Group extends NamedEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String FIND_ALL_QUERY_NAME = "Group.findAll";
	public static final String COUNT_QUERY_NAME = "Group.count";
	
    @OneToMany(targetEntity=MenuAccess.class, mappedBy="group", cascade=CascadeType.ALL)
    @IndexColumn(name="menu_sequence", base=0, nullable=false)
    private List<MenuAccess> menuAccess = new ArrayList<MenuAccess>();

    @OneToMany(mappedBy="group", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    private List<GroupMember> members = Collections.synchronizedList(new ArrayList<GroupMember>()); 
    
    @OneToMany(mappedBy="group", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    private List<GroupAuthorities> authorities = Collections.synchronizedList(new ArrayList<GroupAuthorities>()); 

    public List<MenuAccess> getMenuAccess() {
		return menuAccess;
	}

	public void setMenuAccess(List<MenuAccess> menuAccess) {
		this.menuAccess = menuAccess;
	}
	
	public String getUsers(){
		StringBuilder builder = new StringBuilder();
		String sep = "";
		for(GroupMember gm : members){
			builder.append(sep);
			builder.append(gm.getUser().getUsername());
			sep = ", ";
		}
		return builder.toString();
	}

	public String getGroupAuthorities(){
		StringBuilder builder = new StringBuilder();
		String sep = "";
		for(GroupAuthorities ga: authorities){
			builder.append(sep);
			builder.append(ResourceBundleUtil.getResourceBundleString(ga.getAuthority()));
			sep = ", ";
		}
		return builder.toString();
	}
	
	public List<GroupAuthorities> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(List<GroupAuthorities> authorities) {
		this.authorities = authorities;
	}

	public List<GroupMember> getMembers() {
		return members;
	}

	public void setMembers(List<GroupMember> members) {
		this.members = members;
	}	
}
